home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17453 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: in2.uu.net!zdc!szdc!news
  2. From: braz@ime.usp.br (Rodrigo de Salvo Braz)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: C++ meta-problem
  5. Date: Mon, 15 Apr 1996 17:05:20 GMT
  6. Organization: Zippo
  7. Message-ID: <4ku0vd$lg3@clark.zippo.com>
  8. References: <4jmtlg$smm@baskerville.CS.Arizona.EDU>
  9. NNTP-Posting-Host: ddata116.dialdata.com.br
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. kdb@CS.Arizona.EDU (Koen De Bosschere) wrote:
  13.  
  14. >In order to prevent to write an iterator for every operation 
  15. >I want to perform on a list, I tried to write a kind of 
  16. >meta-iterator that would apply a particular method to 
  17. >all listnodes. For some reason, I cannot execute a 
  18. >function variable on an object. Does anyone has an
  19. >idea how I can solve this problem?
  20.  
  21. >Thanks.
  22.  
  23. >-- Koen De Bosschere
  24.  
  25. >Test program:
  26.  
  27. >#include <iostream.h>
  28.  
  29. >class testclass {
  30. >public:
  31. >  testclass(long d) { data = d; }
  32. >  ~testclass() {} 
  33. >  void print() { cout << data << endl; }
  34. >  testclass *getnext() { return next; }
  35. >private:
  36. >  long data;
  37. >  testclass *next;
  38. >};
  39.  
  40. >// The next function applies the function "f" to all
  41. >// the nodes starting at listnode "root".
  42.  
  43. >void iterate(testclass *root, void (testclass::*f)())
  44. >{
  45. >  testclass *p = root;
  46. >  while (p) { 
  47. >    p->f();      //  <--- here the compiler generates an error (see below)
  48. >    p = p->getnext();
  49. >  }
  50. >}
  51.  
  52. Borland C++ says you cannot take the address of a member function
  53. because it is really different from other kinds of function since it
  54. receives the "this" hidden argument.
  55.  
  56. But in fact that is not the reason you get that bug. You cannot use
  57. p->f() without f being an explicity declared member function of your
  58. testclass.
  59.  
  60. This problem is not trivial. Maybe templates can help you. Check it
  61. out.
  62.  
  63. Cheers,
  64. Rodrigo Braz
  65.  
  66.  
  67.